﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
using NaiveBayesClassifier;

namespace Test
{
    class Program
    {
        static void Main(string[] args)
        {
            const string trainingFileName = "training.txt";
            const string testFileName = "test.txt";
            char[] separator = { ' ', '\t' };

            CTrainingSet trainingSet;
            try
            {
                trainingSet = new CTrainingSet(trainingFileName, separator);
            }
            catch (FileNotFoundException e)
            {
                Console.WriteLine(e.Message);
                Console.ReadKey();
                return;
            }

            CNaiveBayes naiveBayes = new CNaiveBayes("Naive Bayes Classifier", trainingSet);
            //naiveBayes.verbose = true;

            StreamReader sr = new StreamReader(File.OpenRead(testFileName));
            string[] titles;

            titles = sr.ReadLine().Split(separator, StringSplitOptions.RemoveEmptyEntries);
            for (int i = 0; i < titles.Length; ++i)
            {
                naiveBayes.TrainOnAttribute(titles[i]);
            }
            naiveBayes.Train();
            //Console.WriteLine(naiveBayes.Train());

            foreach (string s in titles)
                Console.Write(s + '\t');
            Console.WriteLine("Class");

            do
            {
                string line = sr.ReadLine();
                if (line == null || line.Length == 0)
                    break;
                string[] data = line.Split(separator, StringSplitOptions.RemoveEmptyEntries);

                CBaseInstance instance = CBaseInstance.CreateInstance("", titles, data);
                foreach (string s in data)
                    Console.Write(s + '\t');
                Console.WriteLine(naiveBayes.Classify(instance).GetName());
            } while (true);

            Console.ReadKey();
        }
    }
}
